/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.ims.modules.wechat.dao.ProductCategoryDao;
import com.boxin.ims.modules.wechat.entity.ProductCategory;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 商品店家类别Service
 * @author Jakemanse
 * @version 2013-08-12
 */
@Component
@Transactional(readOnly = true)
public class ProductCategoryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ProductCategoryService.class);
	
	@Autowired
	private ProductCategoryDao productCategoryDao;
	
	@Autowired
	private DaoHelper daoHelper ;
	
	public ProductCategory get(Long id) {
		return productCategoryDao.findOne(id);
	}
	
	public List<ProductCategory> findChildrenByParentId(Long parentId){
		DetachedCriteria dc = productCategoryDao.createDetachedCriteria();
		Disjunction dis = Restrictions.disjunction();
		dis.add(Restrictions.eq("parent.id", parentId));
		dis.add(Restrictions.eq("parent.id", 2L));
		dc.add(dis);
		dc.addOrder(Order.desc("sort"));
		
		return productCategoryDao.find(dc);
	}
	
	
	public Integer getMaxValueByParentId(Long parentId){
		 Integer maxValue = (Integer)daoHelper.queryForObject("com.boxin.ims.wechat.getMaxValueByParentId", parentId);
		return maxValue ;
	}
	
	public List<ProductCategory> findRootCategory(){
		DetachedCriteria dc = productCategoryDao.createDetachedCriteria();
		dc.add(Restrictions.in("parent.id", new Object[]{0L,1L,-1L}));
		dc.addOrder(Order.desc("sort"));
		
		return productCategoryDao.find(dc);
	}
	
	
	
	public Page<ProductCategory> find(Page<ProductCategory> page, ProductCategory productCategory) {
		DetachedCriteria dc = productCategoryDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(productCategory.getName())){
			dc.add(Restrictions.like("name", "%"+productCategory.getName()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", ProductCategory.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("sort"));
		return productCategoryDao.find(page, dc);
	}
	
	
	public List<ProductCategory> findAll() {
		DetachedCriteria dc = productCategoryDao.createDetachedCriteria();
		dc.addOrder(Order.desc("sort"));
		return productCategoryDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductCategory productCategory) {
		productCategoryDao.save(productCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		productCategoryDao.deleteById(id);
	}
	
}
